package cn.lisens.modules.cms.controller;

import java.io.FileNotFoundException;
import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.lisens.modules.cms.model.Customer;
import cn.lisens.modules.cms.service.CustomerService;


@Controller
@RequestMapping("/customer")
public class CustomerController {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private CustomerService customerService;

	@RequestMapping("/get")
	@ResponseBody
	public Customer get(Integer id) {
		Customer theCustomer = customerService.getCustomer(id);
		return theCustomer;
	}
	
	@GetMapping("/list")
	public String listCustomers(Model theModel) throws FileNotFoundException {
		List<Customer> theCustomers = customerService.getCustomers();
		theModel.addAttribute("customers", theCustomers);
		return "modules/cms/list-customers";
	}
	
	@PostMapping("/listData")
	@ResponseBody
	public List<Customer> listData(Customer customer) throws FileNotFoundException {
		List<Customer> list = customerService.getCustomers();
		return list;
	}

	@GetMapping("/showForm")
	public String showFormForAdd(Model theModel) {
		Customer theCustomer = new Customer();
		theModel.addAttribute("customer", theCustomer);
		return "modules/cms/customer-form";
	}

	@PostMapping("/saveCustomer")
	public String saveCustomer(@Valid @ModelAttribute("customer") Customer customer,BindingResult bindingResult,Model model) {
		if (bindingResult.hasErrors()) {
			System.err.println("Returning empSave.jsp page");
			model.addAttribute("msg", bindingResult.getFieldError().getDefaultMessage());
			return "modules/cms/customer-form";
		}
		customerService.saveCustomer(customer);
		return "redirect:/customer/list";
	}
	
	@PostMapping("/save")
	@ResponseBody
	public String save(@Valid @ModelAttribute("customer") Customer customer,BindingResult bindingResult) {
		logger.info("请求的参数："+ customer);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (bindingResult.hasErrors()) {
			return "有错误:"+bindingResult.getFieldError().getDefaultMessage();
		}
		customerService.saveCustomer(customer);
		return "保存成功";
	}

	@GetMapping("/updateForm")
	public String showFormForUpdate(@RequestParam("customerId") int theId, Model theModel) {
		Customer theCustomer = customerService.getCustomer(theId);
		theModel.addAttribute("customer", theCustomer);
		return "modules/cms/customer-form";
	}

	@GetMapping("/delete")
	public String deleteCustomer(@RequestParam("customerId") int theId) {
		customerService.deleteCustomer(theId);
		return "redirect:/customer/list";
	}
}